/**
 * Title : jFamilyTree
 * Description: jFamilyTree
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.commons;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 * File dialog utilities
 * 
 * @author Bernard Bou
 */
public class FileDialogs
{
	/**
	 * Extended file filter
	 * 
	 * @author Bernard Bou
	 */
	static class XFileFilter extends javax.swing.filechooser.FileFilter
	{
		/**
		 * Extensions
		 */
		private String[] theExtensions;

		/**
		 * Description
		 */
		private String theDescription;

		/**
		 * Constructor
		 * 
		 * @param theseExtensions
		 *        extension
		 * @param thisDescription
		 *        description
		 */
		public XFileFilter(String[] theseExtensions, String thisDescription)
		{
			theExtensions = theseExtensions;
			theDescription = thisDescription;
		}

		/*
		 * (non-Javadoc)
		 * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
		 */
		public boolean accept(File thisFile)
		{
			for (String thisExtension : theExtensions)
				if (thisFile.getName().toLowerCase().endsWith(thisExtension))
					return true;
			return thisFile.isDirectory();
		}

		/*
		 * (non-Javadoc)
		 * @see javax.swing.filechooser.FileFilter#getDescription()
		 */
		public String getDescription()
		{
			return theDescription;
		}
	}

	/**
	 * Executable file filter
	 */
	static private FileFilter theExecutableFileFilter = new XFileFilter(new String[] { ".exe", ".sh" }, Messages.getString("FileDialogs.1")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

	/**
	 * XML file filter
	 */
	static private FileFilter theXmlFileFilter = new XFileFilter(new String[] { ".xml" }, Messages.getString("FileDialogs.2")); //$NON-NLS-1$ //$NON-NLS-2$

	/**
	 * XSL file filter
	 */
	static private FileFilter theXslFileFilter = new XFileFilter(new String[] { ".xsl", ".xslt" }, Messages.getString("FileDialogs.3")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

	/**
	 * XSL file filter
	 */
	static private FileFilter thePropertyFileFilter = new XFileFilter(new String[] { ".properties" }, Messages.getString("FileDialogs.3")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

	/**
	 * Get executable
	 * 
	 * @param thisCurrentDirectory
	 *        current directory
	 * @return string for executable
	 */
	static public String getExec(String thisCurrentDirectory)
	{
		JFileChooser thisChooser = makeFileChooser();
		setCurrentDirectory(thisChooser, thisCurrentDirectory);
		thisChooser.setFileFilter(theExecutableFileFilter);
		if (JFileChooser.APPROVE_OPTION == thisChooser.showOpenDialog(null))
			return thisChooser.getSelectedFile().getPath();
		return null;
	}

	/**
	 * Get property file
	 * 
	 * @param thisCurrentDirectory
	 *        current directory
	 * @return string for property file
	 */
	static public String getPropertyFile(String thisCurrentDirectory)
	{
		JFileChooser thisChooser = makeFileChooser();
		setCurrentDirectory(thisChooser, thisCurrentDirectory);
		thisChooser.setFileFilter(thePropertyFileFilter);
		if (JFileChooser.APPROVE_OPTION == thisChooser.showOpenDialog(null))
			return thisChooser.getSelectedFile().getPath();
		return null;
	}

	/**
	 * Get XML file path
	 * 
	 * @param thisCurrentDirectory
	 *        current directory
	 * @return string for XML file path
	 */
	static public String getXml(String thisCurrentDirectory)
	{
		JFileChooser thisChooser = makeFileChooser();
		setCurrentDirectory(thisChooser, thisCurrentDirectory);
		thisChooser.setFileFilter(theXmlFileFilter);
		if (JFileChooser.APPROVE_OPTION == thisChooser.showOpenDialog(null))
			try
			{
				return thisChooser.getSelectedFile().getCanonicalPath();
			}
			catch (IOException thisException)
			{
			}
		return null;
	}

	/**
	 * Get XML url
	 * 
	 * @param thisCurrentDirectory
	 *        current directory
	 * @return string for XML file url
	 */
	static public String getXmlUrl(String thisCurrentDirectory)
	{
		JFileChooser thisChooser = makeFileChooser();
		setCurrentDirectory(thisChooser, thisCurrentDirectory);
		thisChooser.setFileFilter(theXmlFileFilter);
		if (JFileChooser.APPROVE_OPTION == thisChooser.showOpenDialog(null))
			try
			{
				return thisChooser.getSelectedFile().toURI().toURL().toString();
			}
			catch (MalformedURLException thisException)
			{
			}
		return null;
	}

	/**
	 * Get XSL url
	 * 
	 * @param thisCurrentDirectory
	 *        current directory
	 * @return string for XSL file url
	 */
	static public String getXslUrl(String thisCurrentDirectory)
	{
		JFileChooser thisChooser = makeFileChooser();
		setCurrentDirectory(thisChooser, thisCurrentDirectory);
		thisChooser.setFileFilter(theXslFileFilter);
		if (JFileChooser.APPROVE_OPTION == thisChooser.showOpenDialog(null))
			try
			{
				return thisChooser.getSelectedFile().toURI().toURL().toString();
			}
			catch (MalformedURLException thisException)
			{
			}
		return null;
	}

	/**
	 * Get directory
	 * 
	 * @param thisCurrentDirectory
	 *        current directory
	 * @return string for directory path
	 */
	static public String getFolder(String thisCurrentDirectory)
	{
		JFileChooser thisChooser = makeFolderChooser();
		setCurrentDirectory(thisChooser, thisCurrentDirectory);
		if (JFileChooser.APPROVE_OPTION == thisChooser.showOpenDialog(null))
			return thisChooser.getSelectedFile().getPath();
		return null;
	}

	/**
	 * Make file chooser
	 * 
	 * @return file chooser
	 */
	static private JFileChooser makeFileChooser()
	{
		JFileChooser thisChooser = new JFileChooser();
		thisChooser.setDialogTitle(Messages.getString("FileDialogs.0")); //$NON-NLS-1$
		thisChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		return thisChooser;
	}

	/**
	 * Make folder chooser
	 * 
	 * @return folder chooser
	 */
	static private JFileChooser makeFolderChooser()
	{
		JFileChooser thisChooser = new JFileChooser();
		thisChooser.setDialogTitle(Messages.getString("FileDialogs.0")); //$NON-NLS-1$
		thisChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		thisChooser.setFileFilter(new javax.swing.filechooser.FileFilter()
		{
			public boolean accept(File thisFile)
			{
				return thisFile.isDirectory();
			}

			public String getDescription()
			{
				return Messages.getString("FileDialogs.4"); //$NON-NLS-1$
			}
		});
		return thisChooser;
	}

	/**
	 * Set current directory for file chooser
	 */
	/**
	 * @param thisChooser
	 *        file chooser
	 * @param thisCurrentDirectory
	 *        directory to set as current
	 */
	static private void setCurrentDirectory(JFileChooser thisChooser, String thisCurrentDirectory)
	{
		if (thisCurrentDirectory == null || thisCurrentDirectory.isEmpty())
			return;
		File thisDirectory = null;
		if (thisCurrentDirectory.startsWith("file:"))
		{
			try
			{
				thisDirectory = new File(new URI(thisCurrentDirectory));
			}
			catch (URISyntaxException thisException)
			{
			}
		}
		else
			thisDirectory = new File(thisCurrentDirectory);
		thisChooser.setCurrentDirectory(thisDirectory);
	}
}
